/*
package com.changzhi.service.impl;

import com.changzhi.common.constans.Constants;
import com.changzhi.common.constans.RedisConstans;
import com.changzhi.common.util.IdSnowflake;
import com.changzhi.ldyl.model.Activity;
import com.changzhi.ldyl.model.GiftConfig;
import com.changzhi.ldyl.model.vo.LotteryVo;
import com.changzhi.mapper.GiftConfigMapper;
import com.changzhi.service.ActivityService;
import com.changzhi.service.LotteryService;
import com.changzhi.util.CommonUtil;
import com.changzhi.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

*/
/**
 * @Description:
 * @author: 小落
 * @date: 2020年12月15日
 *//*

@Slf4j
@Service
public class LotteryServiceImpl implements LotteryService {

	@Autowired
	private ActivityService activityService;
	@Resource
	private GiftConfigMapper giftConfigMapper;
	@Autowired
	private RedisUtil redisUtil;
	private static Integer USER_TOTAL_ROUND = 99;// 用户抽奖最大回合数-1


	@Override
	public LotteryVo lottery(String userId, Integer times) {
		Long roundId = IdSnowflake.getInstance().nextId();
		// 获取所有抽奖奖品
		List<GiftConfig> prizeList = giftConfigMapper.selectActivityPrize(Constants.CHRISTMAS_PRIZE);
		if(CollectionUtils.isEmpty(prizeList)){
			return null;
		}
		// 计算所有概率的总和
		Integer sumProbability = prizeList.stream().mapToInt(t -> Integer.valueOf(t.getProbability())).sum();
		// 用户抽奖次数
		Integer roundCount = (Integer) redisUtil.hget(RedisConstans.ACTIVITY_LOTTERY_COUNT, userId);
		// 用户已抽奖的奖品总值
		Integer giftValue = (Integer) redisUtil.hget(RedisConstans.ACTIVITY_LOTTERY_PRIZE_VALUE, userId);
		// 我的中奖列表
		List<GiftConfig> myPrizeList = new ArrayList<>();
		for (int i = 0; i < times; i++) {
			if(giftValue == null || (roundCount != null && roundCount > USER_TOTAL_ROUND)){
				giftValue = 0;
			}
			if (roundCount == null || roundCount > USER_TOTAL_ROUND) {
				roundCount = 0;
			}
			//增加抽奖次数
			roundCount++;
			//必出奖品是否为空
			GiftConfig giftConfig = (GiftConfig) redisUtil.hget(RedisConstans.ACTIVITY_LOTTERY_MUST_PRIZE, userId);
			if (giftConfig != null) {// 走[指定概率]获取
				myPrizeList.add(giftConfig);
				giftValue += giftConfig.getValue();
				redisUtil.hdel(RedisConstans.ACTIVITY_LOTTERY_MUST_PRIZE, userId);
				log.error("用户Id:{},抽数:{},当前缓存中抽奖次数为:{},【指定概率】大奖结果为:{},中奖概率:100%", userId, times, roundCount, giftConfig.getName());
			} else {
				GiftConfig gift = CommonUtil.lotteryHigh(prizeList, sumProbability);
				log.warn("用户Id:{},抽数:{},当前抽奖次数:{},中【指定概率】中奖结果为:{},中奖概率:{}", userId, times, roundCount, gift.getName(), gift.getProbability());
				myPrizeList.add(gift);
				// 叠加当前用户抽奖奖品价值
				giftValue += gift.getValue();
				if (roundCount.equals(USER_TOTAL_ROUND) && giftValue != 0) {
					// 检查当前回合为最大回合时，生成对应补偿礼物
					GiftConfig mustGift = checkPriceSpreadAndGet(prizeList);
					redisUtil.hset(RedisConstans.ACTIVITY_LOTTERY_MUST_PRIZE, userId, mustGift,RedisConstans.ONE_MONTH);
					log.warn("====个人抽奖回合结束,99抽奖品价值:{}====", giftValue);
				}
			}
		}
		// 将当前查询数据保存到redis
		redisUtil.hset(RedisConstans.ACTIVITY_LOTTERY_COUNT, userId, roundCount, RedisConstans.ONE_MONTH);
		redisUtil.hset(RedisConstans.ACTIVITY_LOTTERY_PRIZE_VALUE, userId, giftValue, RedisConstans.ONE_MONTH);
		//返回结果
		LotteryVo lotteryVo = new LotteryVo();
		lotteryVo.setUserId(userId).setRoundId(roundId).setMyPrizes(myPrizeList);
		return lotteryVo;
	}

	*/
/**
	 * 判断获得礼物价值和总礼物价值差生成来相应个人的奖池数据，并返回个人奖池的抽奖奖品概率列表
	 *//*

	private GiftConfig checkPriceSpreadAndGet(List<GiftConfig> giftConfigList) {
		Activity activity = activityService.getActivity(4);
		Integer target = Integer.valueOf(activity.getProperty2());
		for (GiftConfig giftConfig : giftConfigList) {
			if(giftConfig.getId().equals(target)){
				return giftConfig;
			}
		}
		return null;
			*/
/*for (GiftConfig giftConfig : diamondPrizes) {
				if (giftValue < 10000) {// 出10w
					if (giftConfig.getValue().equals(99990)) {// 礼物价值
						gift = GiftConfig.getGiftObj(giftConfig);
						gift.setProbabilityDecimal("100");
						break;
					}
				} else if (giftValue >= 10000 && giftValue < 50000) {// 出5w
					if (giftConfig.getValue().equals(50000)) {
						gift = GiftConfig.getGiftObj(giftConfig);
						gift.setProbabilityDecimal("100");
						break;
					}
				} else if (giftValue >= 50000 && giftValue < 70000) {// 出3w
					if (giftConfig.getValue().equals(33440)) {
						gift = GiftConfig.getGiftObj(giftConfig);
						gift.setProbabilityDecimal("100");
						break;
					}
				} else if (giftValue >= 70000 && giftValue < 80000) {// 出2w
					if (giftConfig.getValue().equals(13140)) {
						gift = GiftConfig.getGiftObj(giftConfig);
						gift.setProbabilityDecimal("100");
						break;
					}
				} else if (giftValue >= 80000 && giftValue < 90000) {// 出1w
					if (giftConfig.getValue().equals(5200)) {
						gift = GiftConfig.getGiftObj(giftConfig);
						gift.setProbabilityDecimal("100");
						break;
					}
				} else if (giftValue >= 90000 && giftValue < 95000) {// 出5200
					if (giftConfig.getValue().equals(5200)) {
						gift = GiftConfig.getGiftObj(giftConfig);
						gift.setProbabilityDecimal("100");
						break;
					}
				}
			}*//*

	}
}
*/
